package com.credithc.customers.base.factory;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.po.SystemDictPo;
import com.credithc.customers.base.ro.SystemDictRo;
import com.credithc.customers.base.service.ISystemDictionaryService;
import com.credithc.customers.base.vo.SystemDictVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 字典枚举工厂类
 * @author: denghx
 * @date: 2018/12/12 12:06
 * @param: null
 * @return:
 */
@Component
public class DictionaryFactory {

    public static ISystemDictionaryService dictionaryService;
    private static HashMap<String, List<SystemDictPo>> staticDicMap;

    public static List<SystemDictPo> getSysMap(String type) {
        if (staticDicMap == null || staticDicMap.isEmpty()) {
            staticDicMap = new HashMap<>();
            PageInfo<SystemDictPo> systemDictPoPageInfo = dictionaryService.queryPageResultByExample(new Example(SystemDictPo.class), 1, Integer.MAX_VALUE);
            for (SystemDictPo p : systemDictPoPageInfo.getList()) {
                List<SystemDictPo> systemDictVos = staticDicMap.get(p.getType());
                if (systemDictVos == null) {
                    systemDictVos = new ArrayList<>();
                    staticDicMap.put(p.getType(), systemDictVos);
                }
                systemDictVos.add(p);
            }
        }
        HashMap<String, List<SystemDictPo>> newMap = (HashMap<String, List<SystemDictPo>>) staticDicMap.clone();
        return newMap.get(type);
    }

    public static void refreshDicMap() {
        staticDicMap = null;
    }

    @Autowired
    private void setSmsManagerService(ISystemDictionaryService dictionaryService) {
        DictionaryFactory.dictionaryService = dictionaryService;
    }

    private static String KEY_CODE = "code";
    private static String KEY_NAME = "name";


    public static Map<String, String> getSysDictMap(String type, String keyName) {
        Map<String, String> map = new HashMap<>();
        List<SystemDictPo> dictList = getSysMap(type);
        if (dictList != null && dictList.size() > 0) {
            for (SystemDictPo dict : dictList) {
                if (KEY_CODE.equals(keyName)) {
                    map.put(dict.getCode(), dict.getName());
                } else if (KEY_NAME.equals(keyName)) {
                    map.put(dict.getName(), dict.getCode());
                }
            }
        }
        return map;
    }

    public static PageInfo<SystemDictVo> getDefaultSysDict(SystemDictRo dictonary) {
        PageInfo<SystemDictVo> pageInfo = dictionaryService.selectAll(dictonary);
        return pageInfo;
    }


    public enum Dict {


        /**
         * 订单状态
         */
        STATUS("status") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 客户类别
         */
        CUSTOMER_TYPE("customerType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 证件类型
         */
        ID_TYPE("idType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 平台标识
         */
        PLATFORM_TYPE("platform") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 客户关系类型
         */
        CUSTOMER_ROLE("customerRole") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 三要素核查结果
         */
        ELEMENTS_STATUS("elementsStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 业务信息变更处理状态
         */
        BIZ_STATUS("bizStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 异常信息处理状态
         */
        ABNORMAL_STATUS("abnormalStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 异常信息异常类别
         */
        ABNORMAL_TYPE("abnormalType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 子系统标识
         */
        SUB_SYSTEM_SIGN("subSystemSign") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 系统标识
         */
        SYSTEM_SIGN("subSystemSign") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 客户号来源类型
         */
        CUSTOMER_SOURCE_TYPE("customerSourceType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 通知状态
         */
        NOTICE_STATUS("noticeStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 联系方式类型
         */
        CONTACT_TYPE("contact") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 联系方式类型
         */
        MAPPER_TYPE("mapperType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 账户类型
         */
        ACCOUNT_TYPE("accountType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 账户状态
         */
        ACCOUNT_STATUS("accountStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 产品类型
         */
        PRODUCT_TYPE("productType") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 产品状态
         */
        PRODUCT_STATUS("productStatus") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 归属平台
         */
        PRODUCT_CHANNEL("productChannel") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 系统设置
         */
        SYS_SETTINGS("sysSettings") {
            /**
             * 返回字典Map
             *
             * @return
             */
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_NAME);
            }
        },

        /**
         * 客户角色
         */
        ROLE("role") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 统一登陆验签规则
         */
        UNIFY_CHECK_RULE("unifyCheckRule") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 实名规则
         */
        IDENTIFY_CHECK_RULE("identifyCheckRule") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 发送短信规则
         */
        SEND_SMS_RULE("sendSmsRule") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 统一登陆登录规则
         */
        UNIFY_LOGIN_RULE("unifyLoginRule") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 发送固定验证号码号段
         */
        UNIFY_FIXED_CODE("unifyFixedCode") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },
        /**
         * 手机号白名单(白名单内手机号不限制发送次数,一般用于测试)
         */
        UNIFY_WHITE_LIST("unifyWhiteList") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        },

        /**
         * 统一登陆登录规则
         */
        UNIFY_DOMAIN_NAME("unifyDomainName") {
            @Override
            public Map<String, String> getDictMap() {
                return getSysDictMap(this.getValue(), KEY_CODE);
            }
        };


        /**
         * 字典值
         */
        private String value = "";

        /**
         * 构造
         *
         * @param value
         */
        Dict(String value) {
            this.value = value;
        }


        public String getValue() {
            return this.value;
        }

        /**
         * 返回字典Map
         *
         * @return
         */
        public abstract Map<String, String> getDictMap();
    }

    public static class SysSettings {
        /**
         * 是否开启挡板 定时任务
         *
         * @return
         */
        public static boolean enabledTask() {
            boolean enabled = false;
            Object onoff = Dict.SYS_SETTINGS.getDictMap().get("enabled_task");
            if (onoff != null && "1".equals(StringUtils.trim(onoff.toString()))) {
                enabled = true;
            }
            return enabled;
        }
    }


}

